package com.etc;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordStepTwo {

    public static class WordStepMapper extends Mapper<LongWritable,Text,Text,Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] line = value.toString().split("-");
            //上面得出的结果是c++-c.txt	2，最后需要输出的结果是c++	c.txt--->2  所以下面的代码是将前面结果的空格换成--->
            //replaceAll是替换的意思
            context.write(new Text(line[0]),new Text(line[1].replaceAll("\t","--->")));
        }
    }

    public static class WordReduce extends Reducer<Text, Text, Text, Text> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            // stringbuffer是线程安全的，stringbuilder是非线程安全的，在不涉及线程安全的场景下，stringbuilder更快
            StringBuffer buffer = new StringBuffer();
            for (Text value : values) {
                buffer.append(value).append("\t");
            }
            context.write(new Text(key),new Text(buffer.toString()));
        }
    }


}
